clear all
set more off
	
global dir = "C:\Users\mrueda\Documents\Emory\Papers\Networks_persistance\do_files\do_files_APSA21\post_JOP\Replication_BJPS\"	
cd "$dir"

use "Data\cand_level_persist_rep.dta",clear


gen treat=0
replace treat=1 if margin_victory>0&margin_victory~=.
replace treat=. if margin_victory==.

gen treat_margin_victory=treat*margin_victory



	texdoc close 
	cap erase "$dir/Tables/Table1.tex"
	texdoc init "$dir/Tables/Table1.tex", force

	tex \begin{table}[tbph]
	tex \caption{Effect of donating to an election winner on future donations}\label{tab:donations}
	tex \centering
	tex \begin{tabular}{l c c H} \hline
	tex Outcome:&  Any race  & Mayor & Other races \\
	tex & (1) & (2) & (3) \\ \hline
	tex & & & \\
	
	*Model 1
	foreach x in donate_15any b5{
	
		*Summary statistics for the mean
		quietly: regress `x' treat treat_margin_victory margin_victory , vce(cluster muni_code)
		quietly sum `x' if e(sample)
			local mean_`x' : di %5.3f r(mean)
			local sd_`x' : di %5.3f r(sd) 
		
		*Regressions
		rdrobust `x' margin_victory ,  p(1) vce(cluster muni_code)
		
		*Local's for the table
		local bw_`x' : di %5.2f `e(h_l)'
		local ser_`x' = round(`e(se_tau_rb)',0.001)
		local Neff_`x' = `e(N_h_l)'+`e(N_h_r)'
		local N_`x' = `e(N)'
		local poly_`x' = `e(p)'
		local beta1_`x' : di %5.3f `e(tau_cl)'
		local beta2_`x' : di %5.3f `e(tau_bc)'

		*Confidence intervals
			local ser1_`x' : di %5.3f `e(ci_l_rb)'
			local ser2_`x' : di %5.3f `e(ci_r_rb)'
			
/* HERE*/	local em1_`x' = (`beta1_`x''/`mean_`x'')*100 
			local em1_`x' : di %5.2f `em1_`x''
			
		*P-values
		local pval2_`x' : di %5.3f `e(pv_rb)'
		scalar pval2_`x' = e(pv_rb)
		
		
		regress `x' treat treat_margin_victory margin_victory , vce(cluster muni_code)

		local N_`x' : di %5.0f e(N)
		local R2_`x' : di %5.3f e(r2)

		matrix b = e(b)
		matrix v = e(V)
		matrix res=r(table)
		
		local b1_`x' : di %5.3f b[1,1]
		local se1_`x' : di %5.3f sqrt(v[1,1])
		local p_v_`x' :di %5.3f res[4,1]
		local uci_`x': di %5.3f res[6,1]
		local lci_`x': di %5.3f res[5,1]
	

	}	
	
	*Continue table
	tex \multicolumn{3}{l}{Local linear}&\\

	tex Electoral victory & `beta1_donate_15any' & `beta1_b5' & `beta1_b2b' \\
	tex \ \ \ \ Robust p-value & `pval2_donate_15any' & `pval2_b5' & `pval2_b2b' \\
	tex \ \ \ \ CI 95\%  & [`ser1_donate_15any',`ser2_donate_15any'] & [`ser1_b5',`ser2_b5'] & [`ser1_b2b',`ser2_b2b'] \\
	tex & & & \\
	
	tex \multicolumn{3}{l}{Parametric (linear)}&\\

    tex Electoral victory & `b1_donate_15any' & `b1_b5' & `b1_b2b' \\
	tex \ \ \ \ p-value & `p_v_donate_15any' & `p_v_b5' & `p_v_b2b' \\
	tex \ \ \ \ CI 95\%  & [`lci_donate_15any',`uci_donate_15any'] & [`lci_b5',`uci_b5'] & [`lci_b2b',`uci_b2b'] \\
	tex & & & \\
	
	tex Observations & `N_donate_15any' & `N_b5' & `N_b2b' \\
	tex Bandwidth obs. & `Neff_donate_15any' & `Neff_b5' & `Neff_b2b' \\
	tex Mean & `mean_donate_15any' & `mean_b5' & `mean_b2b' \\
	*tex Effect Mean(\%) & `em1_donate_15any' & `em1_b5' & `em1_b2b' \\
	tex Bandwidth & `bw_donate_15any' & `bw_b5' & `bw_b2b' \\ \hline
	tex \end{tabular}
	tex \parbox{160mm}{ \footnotesize{
	tex Local linear estimates of average treatment effects at the cutoff estimated with triangular kernel weights and optimal MSE bandwidth. 95\% robust confidence intervals and robust p-values with clustering at the municipality level are computed following \cite{calonico_robust_2014}. Parametric linear model specification includes interaction of the treatment with running variable and running variable. Bandwidth obs. denotes the number of observations in the optimal MSE bandwidth.
	tex }
	tex }
	tex \end{table}
	cap texdoc close 
